package com.heima.hellospring.api;

import com.heima.hellospring.constant.REnum;
import com.heima.hellospring.domain.Student;
import com.heima.hellospring.exception.RException;
import com.heima.hellospring.service.StudentService;
import com.heima.hellospring.util.RUtil;
import com.heima.hellospring.vo.QueryObj;
import com.heima.hellospring.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
public class StudentApiController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/list")
    public R<List<Student>> findAll(){
        List<Student> students = studentService.findAll();
        return RUtil.success(students);
    }
    //根据姓名查找学生数据
    @GetMapping("/{name}")
    public Student queryStudent(@PathVariable String name){
        List<Student> students= studentService.findByName(name);
        return students.get(0);
    }

    //根据id查找学生数据
    @GetMapping("/{id}")
    public R<Student> queryStudent(@PathVariable("id") Integer id){
        Student student= studentService.findById(id);
        return RUtil.success(student);
    }

    //新增学生信息
    @PostMapping("/add")
    public R<Student> addStudent(Student student){
        return RUtil.success(studentService.insert(student));
    }

    //修改学生信息
    @PutMapping("/update")
    public R<Student> updateStudent(Student student){
        return RUtil.success(studentService.update(student));
    }

    //删除学生数据
    @DeleteMapping("/delete/{id}")
    public R deleteStudent(@PathVariable Integer id){
        studentService.delete(id);
        return RUtil.success();
    }

    //批量删除学生数据
    @DeleteMapping("/{ids}")
    public R deleteStudents(@PathVariable List<Integer> ids){
        studentService.deleteByIds(ids);
        return RUtil.success();
    }

    //判断用户登录
    @GetMapping("/validateUser")
    public R validateUser(String studentid, String password){
        return RUtil.success(studentService.validateStudentUsername(studentid,password));
    }

    @PostMapping("/getbypage")
    public R<Page<Student>> getStudentPage(@RequestBody QueryObj<Student> qObj){
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Integer pageIndex=0;
        Integer pageSize=10;
        if(qObj==null){
            Pageable pageable=PageRequest.of(pageIndex,pageSize,sort);
            Page<Student> students=studentService.getAll(pageable);
            return RUtil.success(students.getContent(),students.getTotalElements());
        }
        else{
            pageIndex=qObj.getPage()-1;
            pageSize=qObj.getLimit();
            if(qObj.getData() instanceof Student) {
                Student student = (Student) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();
                Example<Student> example = Example.of(student, matcher);
                Page<Student> studentPage=studentService.getAll(example,pageable);
                return RUtil.success(studentPage.getContent(),studentPage.getTotalElements());
            }
            else{
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}
